{-
 Copyright 2022-23, Juspay India Pvt Ltd

 This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License

 as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program

 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY

 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of

 the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
-}
{-# OPTIONS_GHC -Wwarn=ambiguous-fields #-}

module Lib.Payment.Domain.Types.PaymentOrder where

import Data.Aeson
import Kernel.Beam.Lib.UtilsTH (mkBeamInstancesForEnum)
import Kernel.External.Encryption
import qualified Kernel.External.Payment.Interface as Payment
import Kernel.Prelude hiding (show)
import Kernel.Types.Common hiding (id)
import Kernel.Types.Id
import Kernel.Utils.TH (mkHttpInstancesForEnum)
import Lib.Payment.Domain.Types.Common

data PaymentOrderE e = PaymentOrder
  { id :: Id PaymentOrder,
    shortId :: ShortId PaymentOrder,
    requestId :: Maybe Text,
    service :: Maybe Text,
    clientId :: Maybe Text,
    description :: Maybe Text,
    returnUrl :: Maybe Text,
    action :: Maybe Text,
    paymentServiceOrderId :: Text, -- generated by Juspay service
    personId :: Id Person,
    merchantId :: Id Merchant,
    entityName :: Maybe EntityName,
    paymentServiceType :: Maybe PaymentServiceType,
    paymentMerchantId :: Maybe Text,
    amount :: HighPrecMoney,
    currency :: Currency,
    status :: Payment.TransactionStatus,
    paymentLinks :: Payment.PaymentLinks,
    clientAuthToken :: Maybe (EncryptedHashedField e Text),
    clientAuthTokenExpiry :: Maybe UTCTime,
    getUpiDeepLinksOption :: Maybe Bool,
    environment :: Maybe Text,
    createMandate :: Maybe Payment.MandateType,
    isRetried :: Bool,
    isRetargeted :: Bool,
    retargetLink :: Maybe Text,
    mandateMaxAmount :: Maybe HighPrecMoney,
    mandateStartDate :: Maybe UTCTime,
    mandateEndDate :: Maybe UTCTime,
    bankErrorMessage :: Maybe Text,
    bankErrorCode :: Maybe Text,
    serviceProvider :: Payment.PaymentService,
    sdkPayloadDump :: Maybe Value,
    validTill :: Maybe UTCTime,
    createdAt :: UTCTime,
    updatedAt :: UTCTime,
    merchantOperatingCityId :: Maybe (Id MerchantOperatingCity),
    paymentFulfillmentStatus :: Maybe PaymentFulfillmentStatus,
    domainEntityId :: Maybe Text,
    domainTransactionId :: Maybe Text
  }
  deriving (Generic)

data PaymentOrderAPIEntity = PaymentOrderAPIEntity
  { id :: Id PaymentOrder,
    shortId :: ShortId PaymentOrder,
    requestId :: Maybe Text,
    service :: Maybe Text,
    clientId :: Maybe Text,
    description :: Maybe Text,
    returnUrl :: Maybe Text,
    action :: Maybe Text,
    personId :: Id Person,
    merchantId :: Id Merchant,
    entityName :: Maybe EntityName,
    amount :: HighPrecMoney,
    currency :: Currency,
    status :: Payment.TransactionStatus,
    paymentLinks :: Payment.PaymentLinks,
    clientAuthToken :: Maybe Text,
    clientAuthTokenExpiry :: Maybe UTCTime,
    getUpiDeepLinksOption :: Maybe Bool,
    environment :: Maybe Text,
    createMandate :: Maybe Payment.MandateType,
    mandateMaxAmount :: Maybe HighPrecMoney,
    mandateStartDate :: Maybe UTCTime,
    mandateEndDate :: Maybe UTCTime,
    createdAt :: UTCTime,
    updatedAt :: UTCTime
  }
  deriving (Generic, ToJSON, FromJSON, ToSchema)

type PaymentOrder = PaymentOrderE 'AsEncrypted

data PaymentServiceType = Normal | FRFSBooking | FRFSBusBooking | BBPS | FRFSMultiModalBooking | FRFSPassPurchase | ParkingBooking
  deriving (Generic, Eq, Ord, FromJSON, ToJSON, Show, Read, ToSchema, ToParamSchema)

$(mkHttpInstancesForEnum ''PaymentServiceType)
$(mkBeamInstancesForEnum ''PaymentServiceType)
